package com.innovation.client;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;

import com.innovation.message.ServerMessageProto.ServerMessage;

public class TcpClientInitializer extends ChannelInitializer<SocketChannel> {

	@Override
	protected void initChannel(SocketChannel ch) throws Exception {
		ChannelPipeline pipeline = ch.pipeline();
		
		pipeline.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());  
		pipeline.addLast("encoder", new ProtobufEncoder());
		
		pipeline.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());  
		pipeline.addLast("decoder", new ProtobufDecoder(ServerMessage.getDefaultInstance()));
		pipeline.addLast("handler", new TcpClientHandler());
	}
}
